package cn.bin758.h5game.module.game.controller.admin.users;

import cn.bin758.h5game.framework.security.config.SecurityProperties;
import cn.bin758.h5game.module.system.controller.admin.auth.vo.AuthLoginReqVO;
import cn.bin758.h5game.module.system.controller.admin.auth.vo.AuthLoginRespVO;
import cn.bin758.h5game.module.system.controller.admin.user.vo.user.UserCreateReqVO;
import cn.bin758.h5game.module.system.service.auth.AdminAuthService;
import cn.bin758.h5game.module.system.service.permission.PermissionService;
import cn.bin758.h5game.module.system.service.permission.RoleService;
import cn.bin758.h5game.module.system.service.social.SocialUserService;
import cn.bin758.h5game.module.system.service.user.AdminUserService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.bin758.h5game.framework.common.pojo.PageResult;
import cn.bin758.h5game.framework.common.pojo.CommonResult;
import static cn.bin758.h5game.framework.common.pojo.CommonResult.success;

import cn.bin758.h5game.framework.excel.core.util.ExcelUtils;

import cn.bin758.h5game.framework.operatelog.core.annotations.OperateLog;
import static cn.bin758.h5game.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.bin758.h5game.module.game.controller.admin.users.vo.*;
import cn.bin758.h5game.module.game.dal.dataobject.users.GameUsersDO;
import cn.bin758.h5game.module.game.convert.users.GameUsersConvert;
import cn.bin758.h5game.module.game.service.users.GameUsersService;

@Api(tags = "管理后台 - 玩家信息")
@RestController
@RequestMapping("/game/users")
@Validated
public class GameUsersController {

    @Resource
    private GameUsersService usersService;

    @PostMapping("/create")
    @ApiOperation("创建玩家信息")
    @PreAuthorize("@ss.hasPermission('game:users:create')")
    public CommonResult<Long> createUsers(@Valid @RequestBody GameUsersCreateReqVO createReqVO) {
        return success(usersService.createUsers(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新玩家信息")
    @PreAuthorize("@ss.hasPermission('game:users:update')")
    public CommonResult<Boolean> updateUsers(@Valid @RequestBody GameUsersUpdateReqVO updateReqVO) {
        usersService.updateUsers(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除玩家信息")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('game:users:delete')")
    public CommonResult<Boolean> deleteUsers(@RequestParam("id") Long id) {
        usersService.deleteUsers(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得玩家信息")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('game:users:query')")
    public CommonResult<GameUsersRespVO> getUsers(@RequestParam("id") Long id) {
        GameUsersDO users = usersService.getUsers(id);
        return success(GameUsersConvert.INSTANCE.convert(users));
    }

    @GetMapping("/list")
    @ApiOperation("获得玩家信息列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('game:users:query')")
    public CommonResult<List<GameUsersRespVO>> getUsersList(@RequestParam("ids") Collection<Long> ids) {
        List<GameUsersDO> list = usersService.getUsersList(ids);
        return success(GameUsersConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得玩家信息分页")
    @PreAuthorize("@ss.hasPermission('game:users:query')")
    public CommonResult<PageResult<GameUsersRespVO>> getUsersPage(@Valid GameUsersPageReqVO pageVO) {
        PageResult<GameUsersDO> pageResult = usersService.getUsersPage(pageVO);
        return success(GameUsersConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出玩家信息 Excel")
    @PreAuthorize("@ss.hasPermission('game:users:export')")
    @OperateLog(type = EXPORT)
    public void exportUsersExcel(@Valid GameUsersExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<GameUsersDO> list = usersService.getUsersList(exportReqVO);
        // 导出 Excel
        List<GameUsersExcelVO> datas = GameUsersConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "玩家信息.xls", "数据", GameUsersExcelVO.class, datas);
    }

}
